import 'package:flutter/material.dart';
import 'h_dimens.dart';

/// Miaoyi UI框架视觉效果和动画系统
///
/// 定义了与Miaoyi框架一致的动画效果、交互状态变化
class HEffects {
  /// 默认构造函数
  const HEffects({
    required this.dimens,
  });

  /// 尺寸系统
  final HDimens dimens;

  /// 创建默认实例
  factory HEffects.standard() {
    return HEffects(dimens: const HDimens());
  }

  /// 获取按钮悬停变换效果
  ///
  /// 当用户将鼠标悬停在按钮上时，应用此效果
  Matrix4 get buttonHoverTransform => Matrix4.identity()..scale(1.02);

  /// 获取按钮按下变换效果
  ///
  /// 当用户点击按钮时，应用此效果
  Matrix4 get buttonPressedTransform => Matrix4.identity()..scale(0.98);

  /// 按钮默认阴影
  List<BoxShadow> get buttonShadow => [
        BoxShadow(
          color: Colors.black.withOpacity(0.1),
          blurRadius: 4,
          offset: const Offset(0, 2),
        ),
      ];

  /// 按钮悬停阴影（略微增强）
  List<BoxShadow> get buttonHoverShadow => [
        BoxShadow(
          color: Colors.black.withOpacity(0.15),
          blurRadius: 6,
          offset: const Offset(0, 3),
        ),
      ];

  /// 卡片默认阴影
  List<BoxShadow> get cardShadow => [
        BoxShadow(
          color: Colors.black.withOpacity(0.05),
          blurRadius: 5,
          offset: const Offset(0, 1),
        ),
      ];

  /// 卡片悬停阴影（增强）
  List<BoxShadow> get cardHoverShadow => [
        BoxShadow(
          color: Colors.black.withOpacity(0.1),
          blurRadius: 8,
          offset: const Offset(0, 3),
        ),
      ];

  /// 弹出层阴影
  List<BoxShadow> get popupShadow => [
        BoxShadow(
          color: Colors.black.withOpacity(0.2),
          blurRadius: 10,
          offset: const Offset(0, 4),
        ),
      ];

  /// 弹出层出现动画曲线
  Curve get popupCurve => Curves.easeOutCubic;

  /// 弹出层出现动画时长
  Duration get popupDuration => Duration(milliseconds: dimens.animationDurationStandard);

  /// 根据按钮类型获取悬停背景色
  ///
  /// 返回一个函数，接受原始颜色，返回悬停时的颜色
  Color Function(Color) getButtonHoverColor() {
    return (Color baseColor) => Color.lerp(
          baseColor,
          Colors.black,
          0.1,
        )!;
  }

  /// 根据按钮类型获取激活状态（按下）背景色
  ///
  /// 返回一个函数，接受原始颜色，返回激活时的颜色
  Color Function(Color) getButtonActiveColor() {
    return (Color baseColor) => Color.lerp(
          baseColor,
          Colors.black,
          0.2,
        )!;
  }

  /// 获取按钮悬停动画时长
  Duration get buttonHoverDuration => Duration(milliseconds: dimens.animationDurationShort);

  /// 获取按钮点击动画时长
  Duration get buttonPressDuration => Duration(milliseconds: 100);

  /// 获取按钮悬停动画曲线
  Curve get buttonHoverCurve => Curves.easeInOut;

  /// 获取按钮点击动画曲线
  Curve get buttonPressCurve => Curves.easeInOut;

  /// 获取一般交互状态变化动画时长
  Duration get interactionDuration => Duration(milliseconds: dimens.animationDurationShort);

  /// 获取一般交互状态变化动画曲线
  Curve get interactionCurve => Curves.easeInOut;

  /// 焦点变化动画时长
  Duration get focusDuration => Duration(milliseconds: dimens.animationDurationShort);

  /// 卡片悬停变换动画
  Matrix4 get cardHoverTransform => Matrix4.identity()..translate(0.0, -2.0, 0.0);

  /// 应用按钮悬停效果
  Widget applyButtonHoverEffect(Widget child) {
    return _HoverEffectWidget(
      builder: (context, isHovered, isPressed) {
        return AnimatedContainer(
          duration: isPressed ? buttonPressDuration : buttonHoverDuration,
          curve: isPressed ? buttonPressCurve : buttonHoverCurve,
          transform: isPressed
              ? buttonPressedTransform
              : (isHovered ? buttonHoverTransform : Matrix4.identity()),
          child: child,
        );
      },
    );
  }

  /// 应用卡片悬停效果
  Widget applyCardHoverEffect(Widget child) {
    return _HoverEffectWidget(
      builder: (context, isHovered, _) {
        return AnimatedContainer(
          duration: interactionDuration,
          curve: interactionCurve,
          transform: isHovered ? cardHoverTransform : Matrix4.identity(),
          decoration: BoxDecoration(
            boxShadow: isHovered ? cardHoverShadow : cardShadow,
          ),
          child: child,
        );
      },
    );
  }
}

/// 悬停效果小部件
///
/// 用于检测鼠标悬停和点击状态，并构建响应这些状态的小部件
class _HoverEffectWidget extends StatefulWidget {
  const _HoverEffectWidget({
    required this.builder,
  });

  final Widget Function(BuildContext context, bool isHovered, bool isPressed) builder;

  @override
  _HoverEffectWidgetState createState() => _HoverEffectWidgetState();
}

class _HoverEffectWidgetState extends State<_HoverEffectWidget> {
  bool _isHovered = false;
  bool _isPressed = false;

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      onEnter: (_) => setState(() => _isHovered = true),
      onExit: (_) => setState(() => _isHovered = false),
      child: GestureDetector(
        onTapDown: (_) => setState(() => _isPressed = true),
        onTapUp: (_) => setState(() => _isPressed = false),
        onTapCancel: () => setState(() => _isPressed = false),
        child: widget.builder(context, _isHovered, _isPressed),
      ),
    );
  }
} 